<html>
<head>
<style type='text/css'>
body {
   background-color: white;
   margin: 1em 2em 1em 2em;
   font-family: Sans-Serif;
   color: #002;
   line-height: 140%;
   font-size: 12px;
}

h1 {
    font-size: 140%;
}

h2 {
    font-size: 130%;
}

h3 {
    font-size: 120%;
}

h4 {
    font-size: 100%;
    font-style: normal;
    font-weight: bold;
}

h5 {
    font-size: 100%;
    font-style: italic;
    font-weight: normal;
}

pre {
   background-color: #eee;
   padding: 0.5em 0.5em 0.5em 2em;
}

@media print {
   pre {word-wrap:break-word; width:100%;}
} 

ul li,
ol li {
   padding-left: 0.3em;
   /*text-indent: -2em;*/
   margin-bottom: 0.5em;
}

em {
   font-style: normal;
   font-weight: bold;
   text-decoration: underline;
   color: #c40;
}

code {
   font-family: Monospace;
   font-size: 100%;
   color: #c40;
}

a, a * {
   text-decoration: underline;
   color: blue;
   /* border: 0.5px solid #aaa;
   white-space: nowrap;
   padding-right: 0.1em;
   padding-left: 0.1em;
   padding-bottom: -5px; */
}

a code {
   color: blue;
}

img {
   position: relative;
   bottom: -4px;
}

div.headline {
   font-weight: bold;
   font-size: 110%;
}

div.copyright {
   margin-top: 1em;
   border-top: 1px solid black;
   padding-top: 0.5em;
}

div.iris_headline {
   border-bottom: 1px solid black;
   padding-bottom: 0.3em;
}

.LaTeX {
   font-family: Monospace;
   font-size: 100%;
   border: 1px solid #060;
   color: #060;
}

code.LaTeX {
   background-color: white;
   padding: 0.5em 0.5em 0.5em 2em;
}
</style>
</head>

<body>
<div class="iris_headline">IRIS Toolbox Reference Manual</div>




<h2 id="modellang/for">!for...!do...!end</h2>
<div class="headline">For loop for automated creation of model code</div>

<h4 id="abbreviated-syntax-cannot-be-nested">Abbreviated syntax (cannot be nested)</h4>
<pre><code>!for
    ListOfTokens
!do
    Template
!end</code></pre>
<h4 id="full-syntax">Full syntax</h4>
<pre><code>!for
    ?ControlName = ListOfTokens
!do
    Template
!end</code></pre>
<h4 id="description">Description</h4>
<p>Use the '!for...!do...!end' command to specify a template and let the IRIS preparser automatically create multiple instances of the template by iterating over a list of tokens. The preparser cycles over the individual strings from the list; in each iteration, the current string is used to replace all occurences of the control variable in the template. The name of the control name is either implicitly a question mark, '?', in the abbreviated syntax, or any string starting with a question mark and not containing blank spaces, question marks (other than the leading question mark), colons or periods; for example, '?x', '?#', '?NAME+'.</p>
<p>The tokens (text strings) in the list must be separated by commas, blank spaces, or line breaks and they themselves must not contain any of those. In each iteration,</p>
<ul>
<li><p>all occurrences of the control variable in the template are replaced with the currently processed string;</p></li>
<li><p>all occurrences in the template of <code>?.ControlName</code> are replaced with the currently processed string converted to lower case; this option is NOT available with the abbreviated syntax;</p></li>
<li><p>all occurrences in the template of <code>?:ControlName</code> are replaced with the currently processed string converted to upper case; this option is NOT available with the abbreviated syntax;</p></li>
</ul>
<p>The list of tokens can be based on Matlab expressions. Use the <a href="../modellang/pseudosubs.html">pseudosubstitution</a> syntax to this end: Enclose an expression in dollar-square brackets, <code>$[...]$</code>. The expression must evaluate to either a numeric vector, a char vector, or a cell array of numerics and/or strings; the value will be then converted to a comma-separted list.</p>
<h4 id="example">Example</h4>
<p>In a model code file, instead of writing a number of definitions of growth rates like the following ones</p>
<pre><code>dP = P/P{-1} - 1;
dW = W/W{-1} - 1;
dX = X/X{-1} - 1;
dY = Y/Y{-1} - 1;</code></pre>
<p>you can use '!for...!do...!end' as follows:</p>
<pre><code>!for
    P, W, X, Y
!do
    d? = ?/?{-1} - 1;
!end</code></pre>
<h4 id="example-1">Example</h4>
<p>We redo the example 1, but using now the fact that you can have as many variable declaration sections or equation sections as you wish. The '!for...!do...!end' structure can therefore not only produce the equations for you, but also make sure all the growth rate variables are properly declared.</p>
<pre><code>!for
    P, W, X, Y
!do
    !transition_variables
        d?
    !transition_equations
        d? = ?/?{-1} - 1;
!end</code></pre>
<p>The preparser expands this structure as follows:</p>
<pre><code>!transition_variables
    dP
!transition_equations
    dP = P/P{-1} - 1;
!transition_variables
    dW
!transition_equations
    dW = W/W{-1} - 1;
!transition_variables
    dX
!transition_equations
    dX = X/X{-1} - 1;
!transition_variables
    dY
!transition_equations
    dY = Y/Y{-1} - 1;</code></pre>
<p>Obviously, you now do not include the growth rate variables in the section where you declare the rest of the variables.</p>
<h4 id="example-2">Example</h4>
<p>In a model code file, instead of writing a number of autoregression processes like the following ones</p>
<pre><code>X = rhox*X{-1} + ex;
Y = rhoy*Y{-1} + ey;
Z = rhoz*Z{-1} + ez;</code></pre>
<p>you can use '!for...!do...!end' as follows:</p>
<pre><code>!for
    ?# = X, Y, Z
!do
    ?# = rho?.#*?{-1} + e?.#;
!end</code></pre>
<h4 id="example-3">Example</h4>
<p>We redo Example 3, but now for six variables named 'A1', 'A2', 'B1', 'B2', 'C1', 'C2', nesting two '!for...!do...!end' structures one within the other:</p>
<pre><code>!for
    ?letter = A, B, C
!do
    !for
        ?number = 1, 2
    !do
        ?letter?number = rho?.letter?number*?letter?number{-1}
            + e?.letter?number;
    !end
!end</code></pre>
<p>The preparser produces the following six equations:</p>
<pre><code>A1 = rhoa1*A1{-1} + ea1;
A2 = rhoa2*A2{-1} + ea2;
B1 = rhob1*B1{-1} + eb1;
B2 = rhob2*B2{-1} + eb2;
C1 = rhoc1*C1{-1} + ec1;
C2 = rhoc2*C2{-1} + ec2;</code></pre>
<h4 id="example-4">Example</h4>
<p>We use a Matlab expression (the colon operator) to simplify the list of tokens. The following block of code</p>
<pre><code>!for
    1, 2, 3, 4, 5, 6, 7
!do
    a? = a?{-1} + res_a?;
!end</code></pre>
<p>can be simplified as follow:</p>
<pre><code>!for
    $[ 1 : 7 ]$
!do
    a? = a?{-1} + res_a?;
!end</code></pre>

</body>
<div class="copyright">IRIS Toolbox. Copyright &copy; 2007-2014 Jaromir Benes.</div>
</html>
